import { createRouter, createWebHistory } from 'vue-router';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import { useAuthStore } from '@/stores/auth';
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        requiresAuth: false,
        title: '登录',
      }
    },
    {
      path: '/login/forgetpassword',
      name: 'ForgetPassword',
      component: () => import('../views/ForgetPassword.vue'),
      meta: {
        requiresAuth: false,
        title: '忘记密码'
      }
    },
    {
      path: '/register',
      name: 'Register',
      component: Register,
      meta: {
        requiresAuth: false,
        title: '注册',
      }
    },
    {
      path: '/',
      component: () => import('@/layouts/AdminLayout.vue'),
      meta: {
        requiresAuth: true,
      },
      children: [
        {
          path: '',
          redirect: '/dashboard'
        },
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('../views/DashBoard.vue'),
          meta: {
            title: '仪表盘',
            requiresAuth: true
          }
        },
        // 用户管理模块
        {
          path: 'users/list',
          name: 'UserList',
          component: () => import('../views/UserManagement.vue'),
          meta: {
            title: '用户列表'
          }
        },
        {
          path: 'users/add',
          name: 'UserAdd',
          component: () => import('../views/UserAdd.vue'),
          meta: {
            title: '添加用户'
          }
        },
        // 角色管理模块
        {
          path: 'roles/list',
          name: 'RoleList',
          component: () => import('../views/RoleManagement.vue'),
          meta: {
            title: '角色列表'
          }
        },
        {
          path: 'roles/add',
          name: 'RoleAdd',
          component: () => import('../views/RoleAdd.vue'),
          meta: {
            title: '添加角色'
          }
        },
        // 菜单管理模块
        {
          path: 'menus/list',
          name: 'MenuList',
          component: () => import('../views/MenuManagement.vue'),
          meta: {
            title: '菜单列表'
          }
        },
        {
          path: 'menus/add',
          name: 'MenuAdd',
          component: () => import('../views/MenuAdd.vue'),
          meta: {
            title: '添加菜单'
          }
        },
        // 系统设置模块
        {
          path: 'system/config',
          name: 'SystemConfig',
          component: () => import('../views/SystemSettings.vue'),
          meta: {
            title: '系统配置'
          }
        },
        // 日志管理模块
        {
          path: 'logs/operation',
          name: 'OperationLogs',
          component: () => import('../views/SystemLogs.vue'),
          meta: {
            title: '操作日志'
          }
        },
        {
          path: 'profile',
          name: 'Profile',
          component: () => import('../views/Profile.vue'),
          meta: {
            title: '个人中心'
          }
        },
        {
          path: '/content',
          children: [
            {
              name: 'Article',
              path: '/content/articles',
              component: () => import('../views/ArticleManagement.vue'),
              meta: {
                title: '文章管理'
              }
            },
            {
              name: 'FileManagement',
              path: '/content/files',
              component: () => import('../views/FileManagement.vue'),
              meta: {
                title: '文件管理'
              }
            },
            {
              name:'ArticleCategory',
              path:'/content/articlecategory',
              component:()=>import('../views/ArticleCategoryManagement.vue'),
              meta:{
                title:'文章分类管理'
              }
            }
          ]
        }
      ]
    }
  ],
})

router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta && to.meta.title) {
    document.title = to.meta.title + ' - 通用后台管理系统';
  }

  const authStore = useAuthStore();
  if (to.meta.requiresAuth && !authStore.isLoggedIn) {
    // 如果页面需要登录且用户未登录，跳转到登录页面
    next('/login');
  } else if (to.path === '/login' && authStore.isLoggedIn) {
    // 如果用户已登录且访问登录页面，跳转到仪表盘
    next('/dashboard');
  } else {
    // 其他情况正常跳转
    next();
  }
});

export default router
